package com.heer.gymnasium.db;

/**
 * Created by zy on 2017/8/3.
 */


import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.heer.gymnasium.AppHelper;
import com.heer.model.Ad;


public class AdvertisementDao {
    private static DBHelper mHelper = DBHelper.getInstance(AppHelper.context());;
    private static String TAG = "AdvertisementDao";

    public synchronized static void insertAd(Ad adver) {
        try {
            //if(!isExists(adver)){//某条广告如果存在，则不进行重复插入
            SQLiteDatabase db = mHelper.getWritableDatabase();
            db.execSQL(
                    "insert into "
                            + DBHelper.TABLE_NAME_ADVER
                            + "(ad_id,end,title,url,typeid,androidimg,flag) values(?,?,?,?,?,?,?)",
                    new Object[] { adver.getId(),adver.getEnd(),adver.getTitle(),adver.getUrl(),adver.getTypeid(),adver.getAndroidimg(),adver.getFlag()
                    });
            db.close();
            //}
        } catch (Exception e) {
        }
    }

    public synchronized static void deleteAd(Ad adver) {
        try {
            SQLiteDatabase db = mHelper.getWritableDatabase();
            db.delete(DBHelper.TABLE_NAME_ADVER, "ad_id=?", new String[]{adver.getId()});
            db.close();
        } catch (Exception e) {
        }
    }

    /**
     * 获得医生信息
     *
     * @return
     */
    public synchronized static ArrayList<Ad> getAdDataList() {
        ArrayList<Ad> list = new ArrayList<Ad>();
        Cursor cursor = null;
        try {
            SQLiteDatabase db = mHelper.getReadableDatabase();
            cursor = db.rawQuery("select * from " + DBHelper.TABLE_NAME_ADVER
                    + " ", null);
            while (cursor.moveToNext()) {
                Ad info = new Ad();
                info.setId(cursor.getString(cursor.getColumnIndex("ad_id")));
                info.setEnd(cursor.getString(cursor.getColumnIndex("end")));
                info.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                info.setUrl(cursor.getString(cursor.getColumnIndex("url")));
                info.setTypeid(cursor.getString(cursor.getColumnIndex("typeid")));
                info.setAndroidimg(cursor.getString(cursor.getColumnIndex("androidimg")));
                info.setFlag(cursor.getString(cursor.getColumnIndex("flag")));
                list.add(info);
            }
            cursor.close();
            db.close();
        } catch (Exception e) {
        }
        return list;

    }

    /**
     * 查询某条广告信息是否存在
     * @param adver
     * @return
     */
    public synchronized static boolean isExists(Ad adver) {
        boolean exists = false;
        try {
            SQLiteDatabase db = mHelper.getReadableDatabase();
            Cursor cursor = db.rawQuery("select * from " + DBHelper.TABLE_NAME_ADVER
                    + " where ad_id = ? ", new String[] { adver.getId() });
            exists = cursor.moveToNext();
            cursor.close();
            db.close();
        } catch (Exception e) {
        }
        return exists;
    }
}